﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetExtensions.Core.WebApi.Jwt
{
    public class JwtParameter
    {
        public JwtParameter(string name)
        {
            Name = name;
        }
        public JwtParameter(string name, string value)
        {
            Name = name;
            Value = value;
        }
        public string Name { get; set; }
        public string Value { get; set; }
    }

    public class JwtParameters : ValueCollection<JwtParameter>
    {
        public const string Sid = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid";
        public const string Name = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
        public const string Role = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role";
        public const string Expiration = "http://schemas.microsoft.com/ws/2008/06/identity/claims/expiration";
        public const string Expired = "http://schemas.microsoft.com/ws/2008/06/identity/claims/expired";

        public JwtParameters() : base()
        {

        }
        public JwtParameters(List<JwtParameter> values) : base(values)
        {

        }



        public string this[string key]
        {
            get
            {
                if (this.TryGet(x => x.Name == key, out JwtParameter value)) return value.Value;
                return null;
            }
            set {
                if (this.TryGet(x => x.Name == key, out JwtParameter value1))
                    value1.Value = value ;
                else
                    Add(new JwtParameter(key, value));
            }
        }


        public override void Add(JwtParameter item)
        {
            Add(item, (x) => x.Name == item.Name);
        }
    }
}
